using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using CodePlex.SpaceBlock;
using System.Reflection;
using CodePlex.SpaceBlock.UI.Controls;
using CodePlex.SpaceBlock.UI.Controls.Log;
using CodePlex.SpaceBlock.JobModel;

namespace CodePlex.SpaceBlock.UI
{
    public class UIManager
    {
        private static UIManager _instance;

        public static UIManager Instance
        {
            get
            {
                return _instance;
            }

            set
            {
                _instance = value;
            }
        }

        
        private ExceptionForm _exceptionForm;
     
        private readonly ImageList _imageList;
        private readonly ToolStripLabel _statusBarLabel;
        private readonly JobManager _jobManager;
        private readonly MainForm _mainForm;

        public UIManager(ImageList smallImageList, ToolStripLabel statusBarLabel, MainForm mainForm)
        {
            _imageList = smallImageList;
            _statusBarLabel = statusBarLabel;
            _jobManager = new JobManager();
            _mainForm = mainForm;
        }

        public MainForm MainForm
        {
            get
            {
                return _mainForm;
            }
        }

        public ExceptionForm ExceptionForm
        {
            get
            {
                if (_exceptionForm == null)
                {
                    _exceptionForm = new ExceptionForm();
                    _exceptionForm.Icon = this.GetIcon(UIConstants.ImageHints.Information);
                }

                return _exceptionForm;
            }
        }

        public JobManager JobManager
        {
            get
            {
                return _jobManager;
            }
        }

        public static void SetHoverStatus(Control control, string message, params object[] parameters)
        {
            control.MouseEnter += delegate
         {
             UIManager.Instance.SetStatus(message,parameters);
         };
            control.MouseLeave += delegate
            {
                UIManager.Instance.SetStatus("");
            };
        }

        public void SetStatus(string message, params object[] parameters)
        {
            _statusBarLabel.Text = string.Format(message, parameters);
        }

        public ImageList SmallImageList
        {
            get 
            {
                return _imageList;
            }

        }

        public Icon GetIcon(string hint)
        {
            Image image = this.GetSmallImage(hint);

            return UIUtils.GetIcon(image);

        }

        public Image GetSmallImage(string hint)
        {
            if (this.SmallImageList.Images.Count == 0)  // why??
            {
                return null;
            }
            return this.SmallImageList.Images[this.GetSmallImageListIndex(hint)];
        }

        public int GetSmallImageListIndex(string hint)
        {

            Dictionary<string, int> map = new Dictionary<string, int>();

            map[UIConstants.ImageHints.FolderClosed] = 0;
            map[UIConstants.ImageHints.FolderOpen] = 1;
            map[UIConstants.ImageHints.File] = 2;
            map[UIConstants.ImageHints.User] = 3;
            map[UIConstants.ImageHints.UserGroup] = 4;
            map[UIConstants.ImageHints.PropertiesOld] = 5;
            map[UIConstants.ImageHints.Gear] = 6;
            map[UIConstants.ImageHints.ExitArrow] = 7;
            map[UIConstants.ImageHints.Options] = 8;
            map[UIConstants.ImageHints.Information] = 9;
            map[UIConstants.ImageHints.ExpandDown] = 10;
            map[UIConstants.ImageHints.ExpandUp] = 11;
            map[UIConstants.ImageHints.Transfer] = 12;
            map[UIConstants.ImageHints.LogWindow] = 13;
            map[UIConstants.ImageHints.PencilAndPaper] = 14;
            map[UIConstants.ImageHints.NewFolder] = 15;
            map[UIConstants.ImageHints.Delete] = 16;
            map[UIConstants.ImageHints.Refresh] = 17;
            map[UIConstants.ImageHints.Properties] = 18;
            map[UIConstants.ImageHints.SpaceBlock] = 19;
            map[UIConstants.ImageHints.S3] = 20;
            map[UIConstants.ImageHints.Drive] = 21;
            map[UIConstants.ImageHints.CircleAdd] = 22;
            map[UIConstants.ImageHints.CircleRemove] = 23;
            map[UIConstants.ImageHints.Valid] = 24;
            map[UIConstants.ImageHints.Invalid] = 25;
            map[UIConstants.ImageHints.Key] = 26;
            map[UIConstants.ImageHints.WorldLink] = 27;
            map[UIConstants.ImageHints.Nirvanix] = 28;
            map[UIConstants.ImageHints.Note] = 29;
            map[UIConstants.ImageHints.NoteGo] = 30;
            map[UIConstants.ImageHints.Open] = 31;
            map[UIConstants.ImageHints.Rename] = 32;
            map[UIConstants.ImageHints.SSDS] = 33;
            map[UIConstants.ImageHints.Azure] = 34;
            map[UIConstants.ImageHints.SunCloud] = 35;
            return map.ContainsKey(hint) ? map[hint] : 0;
           
        }

        public Image GetSmallImage(Path path, bool isOpen)
        {
            return this.SmallImageList.Images[this.GetSmallImageListIndex(path, isOpen)];
        }

        public int GetSmallImageListIndex(Path path, bool isOpen)
        {
            if (path.IsFolder)
            {
                return GetSmallImageListIndex(isOpen ? UIConstants.ImageHints.FolderOpen : UIConstants.ImageHints.FolderClosed);
            }
            else
            {
                return GetSmallImageListIndex(UIConstants.ImageHints.File);
            }
        }

   
    }
}
